package cn.clx.basic.controller;

import cn.clx.basic.dto.RegisterDto;
import cn.clx.basic.exception.BusinessException;
import cn.clx.basic.service.IVerifyCodeService;
import cn.clx.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 验证码图片
 */
@RestController
@RequestMapping("/verifycode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    /**
     * 返回的是一串数字，经过富文本编译器的一串字符，字符就是一张验证码的图片转换成base64的字符串
     * @param key
     * @return
     */
    @GetMapping("/image/{key}")
    public AjaxResult verifyCode(@PathVariable("key") String key){
        try {
            String base64 = verifyCodeService.verificationCode(key);
            return AjaxResult.getAjaxResult().setResultObj(base64);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false);
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage("程序出错,正在殴打程序员");
        }
    }
    /**
     * 短信验证码的验证
     * @param key 前端传过来的值  电话号码、图形验证码、key值
     * @return
     */
    @PostMapping("/sendSmsCode")
    public AjaxResult sendSmsCode(@RequestBody RegisterDto dto){
        try {
            verifyCodeService.sendSmsCode(dto);
            return AjaxResult.getAjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage("程序出错,正在殴打程序员");
        }
    }

}
